#include "SchemaSettings.h"
SchemaSettings::SchemaSettings(QString ProjectPath,QDialog *parent):QDialog(parent)
{
    QDesktopWidget* desktopWidget = QApplication::desktop();
    //得到客户区矩形
    QRect clientRect = desktopWidget->availableGeometry();
    this->setWindowTitle(tr("Edit Schema Configuration"));
    this->setModal(true);
    this->setModal(true);
    Qt::WindowFlags flags=Qt::Widget;
    //flags |=Qt::WindowMinimizeButtonHint;
    this->setWindowFlags(flags);
    this->setMinimumSize(clientRect.width()*0.6,clientRect.height()*0.6);

    this->mDirPath = ProjectPath;
    this->initUI();
    this->initMethodsFoldList();
    this->setWindowIcon(QIcon(":/QSS/image/Window_Icon.png"));

}

void SchemaSettings::initUI(){
    QGridLayout *MainLayout = new QGridLayout();
    this->setLayout(MainLayout);


    this->AddFold =new QPushButton(tr("Add"));
    this->RemoveFold = new QPushButton(tr("Remove"));
    this->Comfirm = new QPushButton(tr("Confirm"));
    this->MethodsFoldList = new QListWidget();
    this->MethodsFoldList->setEditTriggers(QAbstractItemView::DoubleClicked);
    this->MethodsFoldList->setSelectionMode(QAbstractItemView::ExtendedSelection);
    this->PreviewList = new QListWidget();

    MainLayout->addWidget(AddFold,0,0,1,1);
    MainLayout->addWidget(RemoveFold,0,1,1,1);
    MainLayout->addWidget(Comfirm,0,4,1,1);
    MainLayout->addWidget(MethodsFoldList,1,0,4,3);
    MainLayout->addWidget(PreviewList,1,4,4,2);

    MainLayout->setMargin(30);
    MainLayout->setSpacing(20);

    connect(this->AddFold,SIGNAL(clicked(bool)),this,SLOT(on_AddFold_click()));
    connect(this->RemoveFold,SIGNAL(clicked(bool)),this,SLOT(on_RemoveFold_click()));
    connect(this->Comfirm,SIGNAL(clicked(bool)),this,SLOT(on_Comfirm_click()));
    connect(this->MethodsFoldList,SIGNAL(itemClicked(QListWidgetItem *)),this,SLOT(on_MethodsFoldListItem_click(QListWidgetItem *)));






}

bool SchemaSettings::initMethodsFoldList(){
    this->mConversionMethods = new QList<QString>();
    QString DirPath=mDirPath+"/config/project.xml";
    if(!get_setInfo::readMethodsFold(DirPath,this->mConversionMethods)){
        return false;
    }

    for(int i=0;i<this->mConversionMethods->size();i++){
        QListWidgetItem *MethodsFoldListItem = new QListWidgetItem(this->mConversionMethods->at(i));
        this->MethodsFoldList->addItem(MethodsFoldListItem);
    }

    return true;
}

void SchemaSettings::on_AddFold_click(){
    QString MethodsFoldPath = QFileDialog::getExistingDirectory(this, tr("Open Directory"),
                                                      "/home",
                                                      QFileDialog::ShowDirsOnly
                                                    | QFileDialog::DontResolveSymlinks);
    if(!MethodsFoldPath.isEmpty()){
        QListWidgetItem *mListWidgetItem = new QListWidgetItem(MethodsFoldPath);
        this->MethodsFoldList->addItem(mListWidgetItem);
    }
}

void SchemaSettings::on_Comfirm_click(){
    this->PreviewList->clear();
    QString DirPath=mDirPath+"/config/project.xml";
    int rowsCount = this->MethodsFoldList->count();
    QList<QString> *MethodsFolds = new QList<QString>();
    for(int i=0;i<rowsCount;i++){
        MethodsFolds->append(this->MethodsFoldList->item(i)->text());
    }
    if(MethodsFolds->size()>=0){
        QMessageBox::StandardButton button;
        button = QMessageBox::question(this, tr("Save Settings"),
                                             QString(tr("Sure to save?")),
                                             QMessageBox::Yes | QMessageBox::No);
              if (button == QMessageBox::No) {
                  return ;
              }
              else if (button == QMessageBox::Yes) {
                  if(get_setInfo::setMethodsFold(DirPath,MethodsFolds)){
                      emit SendInfo("Save settings successfully.");
                      this->accept();
                  }
              }
    }
}

void SchemaSettings::on_MethodsFoldListItem_click(QListWidgetItem *mListWidgetItem){
    this->PreviewList->clear();
    this->CurrentListWidgetItem = mListWidgetItem;

    QString MethodPath = mListWidgetItem->text();
    QStringList CMethods;

    QDir sourceDir;
    if(QDir(MethodPath).isRelative()){
       sourceDir=QDir (PathUtil::RelativePathToAbsolutePath(this->mDirPath,MethodPath));
    }else{
        sourceDir=QDir(MethodPath);
    }
            //获取当前目录所有文件
    QFileInfoList fileInfoList =sourceDir.entryInfoList();
    //遍历
    foreach(QFileInfo fileInfo,fileInfoList){
        if(fileInfo.fileName()=="."||fileInfo.fileName()=="..") continue;
        if(QFileInfo (fileInfo.absoluteFilePath()+"\\"+fileInfo.fileName()+".exe").exists()){
             CMethods.append(fileInfo.fileName());
        }
    }
    if(CMethods.size()>0){
        this->PreviewList->addItems(CMethods);
    }


    mListWidgetItem->setFlags(Qt::ItemIsEnabled|Qt::ItemIsSelectable|Qt::ItemIsEditable);

}

void SchemaSettings::on_RemoveFold_click(){
   int rowsCount = this->MethodsFoldList->count();
   QStringList MethodsFolds;
   for(int i=0;i<rowsCount;i++){
       MethodsFolds.append(this->MethodsFoldList->item(i)->text());
   }
   QList<QListWidgetItem *> SelectItems = this->MethodsFoldList->selectedItems();
   for(int i=0;i<SelectItems.size();i++){
       for(int j=0;j<MethodsFolds.size();j++){
         if(SelectItems.at(i)->text()==MethodsFolds.at(j)){
             MethodsFolds.removeAt(j);
             break;
         }
       }
   }
   this->MethodsFoldList->clear();
   this->MethodsFoldList->addItems(MethodsFolds);

}














